import requests
from loguru import logger
import requests
from loguru import logger

from pythonProject1.config import ENV

token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjEsInVzZXJuYW1lIjoiYWRtaW4iLCJpc3MiOiIxMjQuNzAuMzkuMjAxIiwiYXVkIjoiMTI0LjcwLjM5LjIwMSIsImlhdCI6MTc0MjAwNjM2NiwibmJmIjoxNzQyMDA2MzY2LCJleHAiOjE3NDI2MTExNjYsImp0aSI6IjFfYWRtaW4ifQ.h2GRzwyA4MhWZ8XeiVLTEkgipWnjF3FfPzhQuQIonRk"


def request_hook(response, *args, **kwargs):
    logger.debug(f"请求地址：{response.request.url}")
    logger.debug(f"请求头：{response.request.headers}")
    logger.debug(f"请求参数：{response.request.body}")


class RequestBase:
    def __init__(self):
        self._session = requests.Session()
        self._session.headers.update({'Accept': 'application/json', 'token': token})
        self._session.hooks['response'] = request_hook
        self._request = self._session

    def get(self, api_name, url: str, params=None):
        log_data = {
            "method": "get",
            "url": ENV.HOST + url,
            "headers": self._session.headers,
            "params": params
        }
        print(log_data.get("url"))
        logger.debug(f"{api_name}--接口的请求信息为{log_data}")
        res = self._request.get(ENV.HOST + url, params=params)
        return res.json()
